package com.beeantec.jnrerc.modules.common.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.crypto.SecureUtil;
import com.beeantec.jnrerc.core.utils.RedisUtils;
import com.beeantec.jnrerc.modules.common.service.WxService;
import com.beeantec.jnrerc.modules.member.service.MemberService;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;

@Service
public class WxServiceImpl implements WxService {


    @Autowired
    RedisUtils redisUtils;

    @Autowired
    WxService wxService;



    @Value("https://api.weixin.qq.com/")
    String wxBaseUrl;
    @Value("wxsecret")
    String wxSecret;
    @Value("wxappid")
    String wxAppid;

    @Autowired
    MemberService memberService;


    @Override
    public JsonObject getAccessToken(String code) {

        StringBuilder url = new StringBuilder(wxBaseUrl);
        url.append("/sns/oauth2/access_token?appid={appid}&secret={secret}&code={code}&grant_type={grant_type}");
        Map<String, String> uriVariables = new HashMap<String, String>();
        uriVariables.put("appid", wxAppid);
        uriVariables.put("secret", wxSecret);
        uriVariables.put("code", code);
        uriVariables.put("grant_type", "authorization_code");
        HttpEntity<String> requestEntity = new HttpEntity<String>("");
        ResponseEntity<JsonObject> responseEntity = restTemplate.exchange(url.toString(), HttpMethod.GET, requestEntity, JsonObject.class, uriVariables);
            JsonObject result = responseEntity.getBody();
        if (!ObjectUtils.isEmpty(result) && !ObjectUtils.isEmpty(result.get("access_token")) && !ObjectUtils.isEmpty(result.get("openid"))) {
            return result;
        }
            String msg = result.get("errmsg").getAsString();
            log.error("[MemberConf-Error: {}]", msg);
        return null;
    }
    @Override
    public JsonObject getUserInfo(String accessToken, String openid) {
        StringBuilder url = new StringBuilder(wxBaseUrl);
        url.append("/sns/userinfo?access_token={token}&openid={openid}");
        Map<String, String> uriVariables = new HashMap<String, String>();
        uriVariables.put("token", accessToken);
        uriVariables.put("openid", openid);
        HttpEntity<String> requestEntity = new HttpEntity<String>("");
        ResponseEntity<JsonObject> responseEntity = restTemplate.exchange(url.toString(), HttpMethod.GET, requestEntity, JsonObject.class, uriVariables);
            JsonObject result = responseEntity.getBody();
            if (!ObjectUtils.isEmpty(result) && !ObjectUtils.isEmpty(result.get("openid"))) {
                return result;
            }
            String msg = result.get("errmsg").getAsString();
            log.error("[MemberConf-Error: {}]", msg);
         return null;
    }




    public static void main(String[] args) {
        String wxTicket = "LIKLckvwlJT9cWIhEQTwfFziQzGqCwGjdikXCu29xOX8iH524yNdxqbpn6WqF5CV03SjE28RXzVQRHtfGd17Zg";
        long timestamp = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        String noncestr = UUID.fastUUID().toString(true);
        String url = "http://localhost:8083/h5/red-film/f34e2fd87a1d4a3983c3af5a3cf25283";
        String concatStr = "jsapi_ticket=" + wxTicket + "&noncestr=" + noncestr + "&timestamp=" + timestamp + "&url=" + url;
        System.out.println(concatStr);
        String serverSign = SecureUtil.sha1(concatStr);

        System.out.println(serverSign);
    }


    @Autowired
    RestTemplate restTemplate;

    public static Logger log = LoggerFactory.getLogger(WxService.class);


    private JsonObject getUserinfo(String token, String openid) {

        return null;
    }


}
